package com.klz.iblog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.klz.iblog.common.Constants;
import com.klz.iblog.common.Result;
import com.klz.iblog.common.ResultGenerator;
import com.klz.iblog.pojo.Blog;
import com.klz.iblog.pojo.Type;
import com.klz.iblog.service.TypeService;
import com.klz.iblog.vo.TypePageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = {"type接口"})
public class TypeController {

    @Autowired
    private TypeService typeService;

    @ApiOperation("查询所有父类型")
    @GetMapping("/selectParentTypeInfo")
    public Result<Type> selectAllparentTypeInfo() {
        try{
            return ResultGenerator.genSuccessResult(typeService.selectAllparentTypeInfo(Constants.TYPE_PARENT_ID));
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation("根据父id查询所有的子类型")
    @GetMapping("/selectTypeInfo")
    @ApiImplicitParam(name = "typeParentId", value = "父类型id", required = true, dataType = "int", paramType = "query")
    public Result<Type> selectTypeInfoByParentId(@RequestParam("typeParentId") Integer typeParentId) {
        try{
            return ResultGenerator.genSuccessResult(typeService.selectTypeInfoByParentId(typeParentId));
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation("查询该类型博客列表信息")
//    @ApiImplicitParam(name = "TypePageInfo", value = "该类型分页信息", required = true, dataType = "TypePageInfo", paramType = "post")
    @GetMapping("/selectBlogListInfoByType")
    public Result<Type> selectBlogListInfoByType(@RequestBody TypePageInfo typePageInfo) {
        try{
            PageHelper.startPage(typePageInfo.getPage(), typePageInfo.getLimit());
            List<Type> blogList = typeService.selectBlogListInfoByType(typePageInfo.getTypeId());
            PageInfo<Type> pageInfo = new PageInfo<>(blogList);
            return ResultGenerator.genSuccessResult(pageInfo);
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation("按浏览量查询该类型博客列表信息")
//    @ApiImplicitParam(name = "TypePageInfo", value = "按浏览量类型分页信息", required = true, dataType = "TypePageInfo", paramType = "post")
    @GetMapping("/selectBlogListInfoByTypeAndBrowseNum")
    public Result<Type> selectBlogListInfoByTypeAndBrowseNum(@RequestBody TypePageInfo typePageInfo) {
        try{
            PageHelper.startPage(typePageInfo.getPage(), typePageInfo.getLimit());
            List<Type> blogList = typeService.selectBlogListInfoByTypeAndBrowseNum(typePageInfo.getTypeId());
            PageInfo<Type> pageInfo = new PageInfo<>(blogList);
            return ResultGenerator.genSuccessResult(pageInfo);
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }
}
